package com.android.juliomedina.parser;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import com.android.juliomedina.R;
import com.android.juliomedina.DB.Beachbody_DBHelperFeed;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * @author globussoft
 */
public class Beachbody_FeedList extends ListActivity {

	static final String KEY_COST = "cost";
	static final String KEY_DESC = "description";
	static final String KEY_NAME = "name";
	static final String KEY_URL = "url";
	static final String KEY_YOUTUBE = "youtube";
	static final String KEY_IMAGE = "image";
	public static int targetFlag;
	ArrayList<FeedObject> objects;
	String results;

	private class FeedAdapter extends ArrayAdapter<FeedObject> {
		private ArrayList<FeedObject> items;

		public FeedAdapter(Context context, int textViewResource,
				ArrayList<FeedObject> items) {
			super(context, textViewResource, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.items, null);
			}
			TextView title = (TextView) v.findViewById(R.id.title);
			TextView date = (TextView) v.findViewById(R.id.date);
			TextView desc = (TextView) v.findViewById(R.id.desc);

			FeedObject s = items.get(position);

			title.setText(s.getTitle());
			date.setText(s.getDate());
			desc.setText(s.getDesc());

			String stitle = s.getTitle().toString();
			String sdate = s.getDate().toString();
			String sdesc = s.getDesc().toString();

			Beachbody_DBHelperFeed helper = new Beachbody_DBHelperFeed(
					Beachbody_FeedList.this);
			helper.open();
			helper.AddFeed(stitle, sdate, sdesc);

			return v;
		}
	}

	private ArrayList<FeedObject> convertXML(String src) {
		KXmlParser parser = null;
		String name;
		objects = new ArrayList<FeedObject>();
		FeedObject temp = null;

		try {
			parser = new KXmlParser();
			InputStreamReader isr = new InputStreamReader(
					new ByteArrayInputStream(src.getBytes("UTF-8")));
			parser.setInput(isr);

			while (parser.next() != XmlPullParser.END_DOCUMENT) {
				if (parser.getEventType() == XmlPullParser.START_TAG) {
					name = parser.getName();
					if (name.equals("item"))
						temp = new FeedObject();
					else if (name.equals("title"))
						temp.setTitle(parser.nextText());
					else if (name.equals("description")) {
						temp.setDesc(parser.nextText());
					} else if (name.equals("content:encoded")) {
						temp.setContent(parser.nextText());
					}

					else if (name.equals("link"))
						temp.setLink(parser.nextText());
					else if (name.equals("pubDate"))
						temp.setDate(parser.nextText());
				}

				if (parser.getEventType() == XmlPullParser.END_TAG) {
					name = parser.getName();
					if (name.equals("item"))
						objects.add(temp);
				}
			}
			return objects;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			results = extras.getString("results");
		}

		this.setListAdapter(new FeedAdapter(this, R.layout.items,
				convertXML(results)));
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		FeedObject current = objects.get(position);
		String s5 = "";
		String s6 = "";
		String s1 = current.getTitle().toString();
		String s2 = current.getDate().toString();
		String s3 = current.getDesc().toString();
		String s4 = current.getLink().toString();
		if (current.getYoutube() != null) {
			s5 = current.getYoutube().toString();
		}
		if (current.getImage() != null) {
			s6 = current.getImage().toString();
		}
		Intent in = new Intent(getApplicationContext(),
				SingleMenuItemActivity.class);
		in.putExtra(KEY_NAME, s1);
		in.putExtra(KEY_COST, s2);
		in.putExtra(KEY_DESC, s3);
		in.putExtra(KEY_URL, s4);
		in.putExtra(KEY_YOUTUBE, s5);
		in.putExtra(KEY_IMAGE, s6);
		startActivity(in);
	}
}
